const reqlib = require('app-root-path').require
const btbAs = reqlib('/app/common/btb')
const btbKlineUtil = reqlib('/app/common/btb/common/kline')

module.exports = {
  // kdj偏差大的买入
  buyIfKdjUpHigh () {
    const minute15EmaInfo = this.btbItem.minute15EmaInfo || {}
    const hourEma = this.btbItem.hourEmaInfo || {}
    if (
      minute15EmaInfo.kdj && minute15EmaInfo.kdj.J > minute15EmaInfo.kdj.D &&
      minute15EmaInfo.kdj.J >= (minute15EmaInfo.kdj.D * 2) &&
      minute15EmaInfo.macd > 0
    ) {
      if (
        hourEma.kdjDiff > 0 &&
        minute15EmaInfo.macd > 0 &&
        !(hourEma.ema5 < hourEma.ema20 && hourEma.ema10 < hourEma.ema20)
      ) {
        if (minute15EmaInfo.kdjDiff > 2) {
          this.buy = true
          this.buyRate = 1
          this.buyReason = 'buyIfKdjUpHigh'
        }
      }
    }
  },
  buyIfAllGood () {
    const dayEma = this.btbItem.dayEmaInfo || {}
    const hourEma = this.btbItem.hourEmaInfo || {}
    const hour4Ema = this.btbItem.hour4EmaInfo || {}
    const minute15EmaInfo = this.btbItem.minute15EmaInfo || {}
    if (
      (dayEma.macd > 0 && dayEma.kdjDiff > 2 && dayEma.sg > 1 && dayEma.ema10 > dayEma.ema20 && dayEma.ema5 > dayEma.ema10) &&
      (btbAs.ifMacdUpTimes(dayEma.emInfoList || [], 3)) &&
      btbAs.ifMacdUpTimes(hour4Ema.emInfoList || [], 3) &&
      (hour4Ema.macd > 0 && hour4Ema.kdjDiff > 2 && hour4Ema.ema10 > hour4Ema.ema20 && hour4Ema.ema5 > hour4Ema.ema10 && hour4Ema.sg > 1) &&
      (minute15EmaInfo.macd > 0 && minute15EmaInfo.kdjDiff > 2 && minute15EmaInfo.ema10 > minute15EmaInfo.ema20 && minute15EmaInfo.ema5 > minute15EmaInfo.ema10)
    ) {
      if (
        this.btbIndexTotalInfo.dayAverageScore >= 50 &&
        this.btbIndexTotalInfo.hour4AverageScore >= 50 &&
        this.btbIndexTotalInfo.hourAverageScore >= 50
      ) {
        if (hourEma.kdjDiff > 2 && minute15EmaInfo.sg > 1) {
          if (hourEma.macd > 0 && hourEma.ema5 > hourEma.ema10) {
            this.buy = true
            // this.buyRate = 1
            // this.buyRateForce = 0.66
            this.buyReason = 'buyIfAllGood-1'
          }
          if (hourEma.macd > 0 && hourEma.ema5 > hourEma.ema20 && hourEma.ema10 > hourEma.ema20) {
            this.buy = true
            // this.buyRate = 1
            // this.buyRateForce = 0.66
            this.buyReason = 'buyIfAllGood-2'
          }
        }
      }
    }
  },
  buyIfMacdKeepTimesLong () {
    const btbItem = this.btbItem
    const minute15EmaInfo = this.btbItem.minute15EmaInfo || {}
    const dayEmaInfo = this.btbItem.dayEmaInfo || {}
    const hourEmaInfo = this.btbItem.hourEmaInfo || {}
    const hour4EmaInfo = this.btbItem.hour4EmaInfo || {}
    if (
      minute15EmaInfo.sg > 1 &&
      minute15EmaInfo.kdjDiff > 0 &&
      btbAs.ifMacdUpTimes(btbItem.emInfoList || [], 20, 1) &&
      minute15EmaInfo.macd > 0 &&
      (minute15EmaInfo.ema10 > minute15EmaInfo.ema20 && minute15EmaInfo.ema5 > minute15EmaInfo.ema10)
    ) {
      if (
        (dayEmaInfo.kdjDiff > 0 && dayEmaInfo.macd > 0) &&
        (hourEmaInfo.kdjDiff > 0)
      ) {
        this.buy = true
        this.buyRate = 1
        // this.buyRateForce = 0.66
        this.buyReason = 'buyIfMacdKeepTimesLong'
      }
    }
    if (
      hourEmaInfo.sg > 1 &&
      hourEmaInfo.kdjDiff > 0 &&
      btbAs.ifMacdUpTimes(hourEmaInfo.emInfoList || [], 20, 1) &&
      hourEmaInfo.macd > 0 &&
      (hourEmaInfo.ema10 > hourEmaInfo.ema20 && hourEmaInfo.ema5 > hourEmaInfo.ema10)
    ) {
      if (
        (dayEmaInfo.kdjDiff > 0 && dayEmaInfo.macd > 0) &&
        (
          hour4EmaInfo.kdjDiff > 0 &&
          hour4EmaInfo.macd > 0 &&
          (hour4EmaInfo.ema10 > hour4EmaInfo.ema20 && hour4EmaInfo.ema5 > hour4EmaInfo.ema10)
        )
      ) {
        if (
          minute15EmaInfo.sg > 1 &&
          minute15EmaInfo.kdjDiff > 0 &&
          minute15EmaInfo.macd > 0 &&
          (minute15EmaInfo.ema10 > minute15EmaInfo.ema20 && minute15EmaInfo.ema5 > minute15EmaInfo.ema10)
        ) {
          this.buy = true
          this.buyRate = 1
          // this.buyRateForce = 0.66
          this.buyReason = 'buyIfMacdKeepTimesLong-2'
        }
      }
    }
  },
  buyIfMinute15EmTimesLong () {
    const minute15EmaInfo = this.btbItem.minute15EmaInfo || {}
    const hourEmaInfo = this.btbItem.hourEmaInfo || {}
    const hour4EmaInfo = this.btbItem.hour4EmaInfo || {}
    if (
      btbAs.ifFuncTimes((em) => {
        return em.ema5 > em.ema20 && em.ema10 > em.ema20
      }, minute15EmaInfo.emInfoList || [], 15) &&
      minute15EmaInfo.kdjDiff > 0 &&
      minute15EmaInfo.macd > 0 &&
      !btbAs.ifFuncTimes((em) => {
        return em.macd > 0
      }, minute15EmaInfo.emInfoList || [], 6) &&
      (minute15EmaInfo.ema10 > minute15EmaInfo.ema20 && minute15EmaInfo.ema5 > minute15EmaInfo.ema10) &&
      hourEmaInfo.macd > 0 &&
      (hourEmaInfo.ema10 > hourEmaInfo.ema20 && hourEmaInfo.ema5 > hourEmaInfo.ema10) &&
      hour4EmaInfo.macd > 0 &&
      (hour4EmaInfo.ema10 > hour4EmaInfo.ema20 && hour4EmaInfo.ema5 > hour4EmaInfo.ema10)
    ) {
      this.buy = true
      this.buyRate = 1
      // this.buyRateForce = 0.66
      this.buyReason = 'buyIfMinute15EmTimesLong'
    }
  },
  buyIfVolumeEnlarge () {
    const minute15EmaInfo = this.btbItem.minute15EmaInfo || {}
    const hourEmaInfo = this.btbItem.hourEmaInfo || {}
    const minute15EmaInfoEnlargeRate = btbKlineUtil.getVolumeEnlargeRate(minute15EmaInfo)
    const hourEmaInfoEnlargeRate = btbKlineUtil.getVolumeEnlargeRate(hourEmaInfo)
    if (
      minute15EmaInfo.kdjDiff > 0 &&
      minute15EmaInfo.macd > 0 &&
      (minute15EmaInfo.ema10 > minute15EmaInfo.ema20 && minute15EmaInfo.ema5 > minute15EmaInfo.ema10)
    ) {
      if (
        minute15EmaInfoEnlargeRate.last > 8 &&
        minute15EmaInfoEnlargeRate.av > 6 &&
        btbKlineUtil.getRealAmplitudeRate(minute15EmaInfo) > 0.7
      ) {
        this.buy = true
        this.buyRate = 1
        this.buyReason = 'buyIfVolumeEnlarge'
        this.buyRateForce = 0.66
        if (minute15EmaInfoEnlargeRate.last > 10) {
          this.buyRateForce = 1
        }
        if (minute15EmaInfoEnlargeRate.last > 18) {
          this.buyRateForce = 1
          this.fullPosition = true
        }
      }
      if (
        hourEmaInfo.kdjDiff > 0 &&
        hourEmaInfoEnlargeRate.last > 8 &&
        hourEmaInfoEnlargeRate.av > 6 &&
        btbKlineUtil.getRealAmplitudeRate(hourEmaInfo) > 0.7
      ) {
        this.buy = true
        this.buyRate = 1
        this.buyReason = 'buyIfVolumeEnlarge-1'
        this.buyRateForce = 0.66
        if (hourEmaInfoEnlargeRate.last > 10) {
          this.buyRateForce = 1
        }
        if (hourEmaInfoEnlargeRate.last > 18) {
          this.buyRateForce = 1
          this.fullPosition = true
        }
      }
    }
  },
  buyIDaysGood () {
    const dayEmaInfo = this.btbItem.dayEmaInfo || {}
    if (!(dayEmaInfo.macd > 0 && (dayEmaInfo.ema5 > dayEmaInfo.ema20 && dayEmaInfo.ema10 > dayEmaInfo.ema20))) {
      return false
    }
    const minute15EmaInfo = this.btbItem.minute15EmaInfo || {}
    const hourEmaInfo = this.btbItem.hourEmaInfo || {}
    if (
      minute15EmaInfo.sg > 1 &&
      minute15EmaInfo.kdjDiff > 0 &&
      minute15EmaInfo.macd > 0 &&
      (minute15EmaInfo.ema10 > minute15EmaInfo.ema20 && minute15EmaInfo.ema5 > minute15EmaInfo.ema20)
    ) {
      if (
        hourEmaInfo.kdjDiff > 0 &&
        hourEmaInfo.macd > 0 &&
        (hourEmaInfo.ema10 > hourEmaInfo.ema20 && hourEmaInfo.ema5 > hourEmaInfo.ema20)
      ) {
        this.buy = true
        this.buyRate = 1
        this.buyReason = 'g-d-b-1'
      }
      if (
        hourEmaInfo.kdjDiff > 0 &&
        hourEmaInfo.macd > 0 &&
        (hourEmaInfo.ema20 > hourEmaInfo.ema10 && hourEmaInfo.ema5 > hourEmaInfo.ema10)
      ) {
        this.buy = true
        this.buyRate = 1
        this.buyReason = 'g-d-b-2'
      }
    }
  },
  // 币的sg越大，行情越确定，大行情的日sg都很大
  buyIfEmaLongSgHigh () {
    const dayEmaInfo = this.btbItem.dayEmaInfo || {}
    const hour4EmaInfo = this.btbItem.hour4EmaInfo || {}
    const hourEmaInfo = this.btbItem.hourEmaInfo || {}
    const minute15EmaInfo = this.btbItem.minute15EmaInfo || {}
    if (
      dayEmaInfo.macd > 0 &&
      dayEmaInfo.kdjDiff > 0 &&
      (dayEmaInfo.ema10 > dayEmaInfo.ema20 && dayEmaInfo.ema5 > dayEmaInfo.ema10) &&
      dayEmaInfo.sg > 17 &&
      btbAs.ifFuncTimes((em) => {
        return em.ema5 > em.ema20 && em.ema10 > em.ema20 && em.macd > 0
      }, dayEmaInfo.emInfoList || [], 6)
    ) {
      if (
        hour4EmaInfo.macd > 0 &&
        (hour4EmaInfo.ema10 > hour4EmaInfo.ema20 && hour4EmaInfo.ema5 > hour4EmaInfo.ema10) &&
        hour4EmaInfo.sg > 2
      ) {
        if (
          hourEmaInfo.macd > 0 &&
          (hourEmaInfo.ema10 > hourEmaInfo.ema20 && hourEmaInfo.ema5 > hourEmaInfo.ema10) &&
          hourEmaInfo.sg > 1
        ) {
          if (
            minute15EmaInfo.kdjDiff > 0 &&
            minute15EmaInfo.macd > 0 &&
            (minute15EmaInfo.ema10 > minute15EmaInfo.ema20 && minute15EmaInfo.ema5 > minute15EmaInfo.ema10)
          ) {
            this.setBuy('e-l-s-g-2')
          }
        }
      }
    }
    if (
      dayEmaInfo.macd > 0 &&
      dayEmaInfo.kdjDiff > 0 &&
      (dayEmaInfo.ema10 > dayEmaInfo.ema20 && dayEmaInfo.ema5 > dayEmaInfo.ema10) &&
      dayEmaInfo.sg > 14 &&
      btbAs.ifFuncTimes((em) => {
        return em.ema5 > em.ema20 && em.ema10 > em.ema20 && em.macd > 0
      }, dayEmaInfo.emInfoList || [], 20)
    ) {
      if (
        hour4EmaInfo.macd > 0 &&
        (hour4EmaInfo.ema10 > hour4EmaInfo.ema20 && hour4EmaInfo.ema5 > hour4EmaInfo.ema10) &&
        hour4EmaInfo.sg > 2
      ) {
        if (
          hourEmaInfo.macd > 0 &&
          (hourEmaInfo.ema10 > hourEmaInfo.ema20 && hourEmaInfo.ema5 > hourEmaInfo.ema10) &&
          hourEmaInfo.sg > 1
        ) {
          if (
            minute15EmaInfo.kdjDiff > 0 &&
            minute15EmaInfo.macd > 0 &&
            (minute15EmaInfo.ema10 > minute15EmaInfo.ema20 && minute15EmaInfo.ema5 > minute15EmaInfo.ema10)
          ) {
            this.setBuy('e-l-s-g-1')
          }
        }
      }
      if (
        btbAs.ifFuncTimes((em) => {
          return em.ema5 > em.ema20 && em.ema10 > em.ema20 && em.macd > 0
        }, hour4EmaInfo.emInfoList || [], 10) &&
        hour4EmaInfo.kdjDiff > 0 &&
        hour4EmaInfo.kdjud > 5 &&
        hour4EmaInfo.sg > 7
      ) {
        if (
          btbAs.ifFuncTimes((em) => {
            return em.ema5 > em.ema20 && em.ema10 > em.ema20
          }, hourEmaInfo.emInfoList || [], 20) &&
          hourEmaInfo.macd > 0 &&
          hourEmaInfo.macdud > 5 &&
          hourEmaInfo.kdjDiff > 0 &&
          hourEmaInfo.kdjud > 5 &&
          hourEmaInfo.sg > 3
        ) {
          if (
            minute15EmaInfo.kdjDiff > 0 &&
            minute15EmaInfo.kdjud >= 5 &&
            minute15EmaInfo.isMacdIncrease &&
            btbAs.ifFuncTimes((em) => {
              return em.ema5 > em.ema20 && em.ema10 > em.ema20
            }, minute15EmaInfo.emInfoList || [], 3)
          ) {
            this.setBuy('e-l-s-g-3')
          }
        }
      }
    }
  }
}
